#include <stdio.h>
#include <time.h>
#include <unistd.h>
#include <string.h>
#include <sys/socket.h>
#include "socklib.h"

#define LEN 256

void process_request(int fd);

static int cnt = 0;

int main()
{
    int sock_id = make_server_socket(4000);
    printf("Create server...\n");
    if (sock_id == -1){
        perror("make_server_socket");
        return -1;
    }
    while(1){
        int fd;
        fd = accept(sock_id,NULL,NULL);
        if (fd == -1){
            perror("accept");
            break;
        }
        process_request(fd);
        close(fd);
    }

    return 0;
}

void process_request(int fd)
{
    //time_t now;
    //char *cp;
    //time(&now);
    //cp = ctime(&now);
	////printf("%s\nlen: %d\n",cp,strlen(cp));
    //write(fd,cp,strlen(cp));

    char buf[LEN];
    int n = recvMsg(fd, buf, LEN);
    if (n < 0) {
        return;
    }
    printf("Have Recv (%d) Msg: %s\n", ++cnt, buf);
    sendMsg(fd, buf);
}

